/*
 * @Author: 邹威鹏(944740645@qq.com)
 * @remarks:
 */
import { children } from '../../../utils/utils'
import echarts from '../../ec-canvas/echarts'
// 全局变量
const app = getApp()
let myChart = null
let option = {}
// get dpr 解决canvas模糊问题
const getPixelRatio = () => {
	let pixelRatio = 0
	wx.getSystemInfo({
		success: function (res) {
			pixelRatio = res.pixelRatio
		},
		fail: function () {
			pixelRatio = 0
		},
	})
	return pixelRatio
}
// 初始化图表
const initChart = (canvas, width, height) => {
	myChart = echarts.init(canvas, null, {
		width: width,
		height: height,
		devicePixelRatio: getPixelRatio(),
	})
	canvas.setChart(myChart)
	myChart.showLoading()
	myChart.setOption(option)
	myChart.hideLoading()
	console.log('初始化图表完成')
	return myChart
}
Page({
	data: {
		curChildrenHealthData: null,
		ec: {
			onInit: initChart,
		},
	},
	onLoad() {
		const eventChannel = this.getOpenerEventChannel()
		// 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
		eventChannel.on('sendChildrenData', res => {
			console.log('sendChildrenData', res)
			this.setData({
				curChildrenHealthData: res.data,
				children: res.children,
			})
		})
	},

	onReady() {
		setTimeout(() => {
			this.getHealthData()
		}, 500)
	},
	// 2、进行初始化数据
	getHealthData() {
		// console.log('加载异步数据')
		let heigthArr = []
		let weightArr = []
		// let wenduArr = []
		// 初始化echarts图
		for (let i = 0; i < this.data.curChildrenHealthData.length; i++) {
			const item = this.data.curChildrenHealthData[i]
			heigthArr.splice(0,0,item.height.match(/\d+/g)[0]);
			weightArr.splice(0,0,item.weight.match(/\d+/g)[0]);
		}
		console.log(heigthArr, weightArr)
		option = {
			title: {
				text: '学生成长数据图',
				subtext: '数据来自kenput公司',
				left: 'center',
				align: 'right',
			},
			tooltip: {
				trigger: 'axis',
				confine: true,
				axisPointer: {
					type: 'cross',
				},
			},
			xAxis: {
				type: 'category',
				data: [
					'一月',
					'二月',
					'三月',
					'四月',
					'五月',
					'六月',
					'七月',
					'八月',
					'九月',
					'十月',
					'十一月',
					'十二月',
				],
			},
			yAxis: {
				type: 'value',
				// name: '单位(kg/cm)',
			},
			legend: {
				data: ['脚底搓球', '双脚连续跳踩球'],
				left: 10,
				top:250
			},
			series: [
				{
					data: heigthArr,
					type: 'line',
					name: '脚底搓球',
				},
				{
					data: weightArr,
					type: 'line',
					name: '双脚连续跳踩球',
				},
			],
		}
		myChart.setOption(option)
	},
})
